﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TB.Web.UI.WebControls;

namespace TB.Web.UI.TreeGridDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Builder.InitTreeGridManual(this.TreeGrid);
            }

        }

        protected void TreeGrid_SelectedNodeChanged(object sender, TB.Web.UI.WebControls.TreeGridNodeEventArgs e)
        {

        }
        protected void TreeGrid_RowCommand(object sender, TB.Web.UI.WebControls.TreeGridRowCommandEventArgs e)
        {
            this.LblHint.Text = "我被点击了[Event Name:" +  e.CommandName+ "] " + DateTime.Now.ToString();

            if (e.CommandName == "Edit")
            {
                TreeGridNode oNode = this.TreeGrid.EditingNode;     // previous EditingNode
                e.Row.Owner.Edit();                                 // set current EditingNode

                if (oNode != null)                                  // cancel previous EditingNode
                {
                    oNode.DataItem = Builder.GetNodeData(oNode.Value);
                    oNode.RaiseNodeDataBind(false);
                }

                e.Row.Owner.DataItem = Builder.GetNodeData(e.Row.Owner.Value);  // bind data
                e.Row.Owner.RaiseNodeDataBind(false);
            }

            if (e.CommandName == "Update")
            {
                e.Row.Owner.Editing = false;

                string newValue = ((TextBox)e.Row.FindControl("TxtSixth")).Text;
                // save .....
                e.Row.Owner.DataItem = Builder.GetNodeData(e.Row.Owner.Value);
                ((Foo)e.Row.Owner.DataItem).C6 = newValue;      // for demo
                e.Row.Owner.RaiseNodeDataBind(false);

                
            }

            if (e.CommandName == "Cancel")
            {
                e.Row.Owner.Editing = false;

                e.Row.Owner.DataItem = Builder.GetNodeData(e.Row.Owner.Value);
                e.Row.Owner.RaiseNodeDataBind(false);
            }

            if (e.CommandName == "Remove")
            {
                if (e.Row.Owner.Parent != null)
                {
                    e.Row.Owner.Parent.ChildNodes.Remove(e.Row.Owner);
                }
                else
                {
                    this.TreeGrid.Nodes.Remove(e.Row.Owner);
                }
            }
        }
        protected void TreeGrid_NodePopulate(object sender, TB.Web.UI.WebControls.TreeGridNodeEventArgs e)
        {
            TreeGridNode node = new TreeGridNode("new1");
            node.DataItem = Builder.GetNodeData("-1");
            e.Node.ChildNodes.Add(node);
            node.PopulateOnDemand = true;
            node.RaiseNodeDataBind(false);

            node = new TreeGridNode("new2");
            node.DataItem = Builder.GetNodeData("-1");
            e.Node.ChildNodes.Add(node);
            node.PopulateOnDemand = true;
            node.RaiseNodeDataBind(false);
        }

        protected void TreeGrid_NodeDataBound(object sender, TreeGridRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Edit)
                {
                    ((LinkButton)e.Row.FindControl("LbtnUpdate")).Visible = true;
                    ((LinkButton)e.Row.FindControl("LbtnCancle")).Visible = true;
                }

                if (e.Row.RowState == DataControlRowState.Normal)
                {
                    ((LinkButton)e.Row.FindControl("LbtnUpdate")).Visible = false;
                    ((LinkButton)e.Row.FindControl("LbtnCancle")).Visible = false;
                }
            }
        }
    }
}
